/*
 * TransaksiOpenView.java
 *
 * Created on July 31, 2008, 11:12 PM
 */
package com.indragunawan.restobiz.app;

import com.indragunawan.restobiz.app.model.TransaksiOpenRD;
import com.indragunawan.restobiz.app.model.TransaksiOpenTM;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import org.jdesktop.application.Action;

/**
 *
 * @author  igoens
 */
public class TransaksiOpenView extends javax.swing.JDialog {
    
    private static final long serialVersionUID = 3267106882638869141L;

    private boolean openTransaction;
    private EntityManager transaksiEntity;
    private String tanggalTransaksi;
    private Integer nomorTransaksi;
    private String openedCustomer;
    private GeneralConfig cfg = new GeneralConfig();
    private int i;

    /** Creates new form TransaksiOpenView */
    public TransaksiOpenView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        tanggalTransaksi = null;
        nomorTransaksi = null;
        openedCustomer = null;

        transaksiEntity = Persistence.createEntityManagerFactory("RestobizPU", cfg.getPersistanceDbProperties()).createEntityManager();
        formatOpenTable();
        setPostedVisibility();
    }

    public boolean getConfirmOpen() {
        return this.openTransaction;
    }

    private void formatOpenTable() {
        if (postedCheckBox.isSelected()) {
            openTable.setModel(new TransaksiOpenTM(openPostedTransaksi()));
        } else {
            openTable.setModel(new TransaksiOpenTM(openDaftarTransaksi()));
        }
        for (i = 0; i < openTable.getColumnCount(); i++) {
            openTable.getColumnModel().getColumn(i).setCellRenderer(new TransaksiOpenRD());
        }
    }

    private List openDaftarTransaksi() {
        List transaksiList = null;
        try {
            transaksiList = transaksiEntity.createQuery("Select t.transaksiPK.tanggal, t.transaksiPK.nomor, t.customer FROM Transaksi t WHERE t.posted = 0").getResultList();
        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        return transaksiList;
    }

    private List openPostedTransaksi() {
        List transaksiList = null;
        try {
            transaksiList = transaksiEntity.createQuery("Select t.transaksiPK.tanggal, t.transaksiPK.nomor, t.customer FROM Transaksi t WHERE t.transaksiPK.tanggal = :tanggal AND t.posted = 1").setParameter("tanggal", postedDateField.getDate()).getResultList();
        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        return transaksiList;
    }

    private void setConfirmOpen(boolean b) {
        this.openTransaction = b;
    }

    public String getTanggalTransaksi() {
        return this.tanggalTransaksi;
    }

    private void setOpenedCustomer(Object Customer) {
        this.openedCustomer = String.valueOf(Customer);
    }

    public String getOpenedCustomer() {
        return this.openedCustomer;
    }

    private void setOpenedDate(Object Tanggal) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        this.tanggalTransaksi = sdf.format(Tanggal);
    }

    public Integer getNomorTransaksi() {
        return this.nomorTransaksi;
    }

    private void setOpenedInvoice(Object Invoice) {
        String i = String.valueOf(Invoice);
        this.nomorTransaksi = Integer.valueOf(i);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contentPanel = new javax.swing.JPanel();
        openScrollPanel = new javax.swing.JScrollPane();
        openTable = new javax.swing.JTable();
        controlPanel = new javax.swing.JPanel();
        batalButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        postedCheckBox = new javax.swing.JCheckBox();
        postedDateField = new org.jdesktop.swingx.JXDatePicker();
        postedOpenButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getResourceMap(TransaksiOpenView.class);
        setTitle(resourceMap.getString("transaksiOpen.title")); // NOI18N
        setName("transaksiOpen"); // NOI18N

        contentPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        contentPanel.setName("contentPanel"); // NOI18N

        openScrollPanel.setName("openScrollPanel"); // NOI18N

        openTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tanggal", "Invoice", "Customer"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Integer.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        openTable.setColumnSelectionAllowed(true);
        openTable.setName("openTable"); // NOI18N
        openTable.getTableHeader().setReorderingAllowed(false);
        openTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                openTableMouseClicked(evt);
            }
        });
        openScrollPanel.setViewportView(openTable);
        openTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        openTable.getColumnModel().getColumn(0).setMaxWidth(100);
        openTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("openTable.columnModel.title0")); // NOI18N
        openTable.getColumnModel().getColumn(1).setMaxWidth(50);
        openTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("openTable.columnModel.title1")); // NOI18N
        openTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("openTable.columnModel.title2")); // NOI18N

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(openScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                .addContainerGap())
        );
        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(openScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                .addContainerGap())
        );

        controlPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        controlPanel.setName("controlPanel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getActionMap(TransaksiOpenView.class, this);
        batalButton.setAction(actionMap.get("batalBukaTransaksi")); // NOI18N
        batalButton.setMnemonic('B');
        batalButton.setText(resourceMap.getString("batalButton.text")); // NOI18N
        batalButton.setName("batalButton"); // NOI18N
        batalButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                batalButtonKeyPressed(evt);
            }
        });

        okButton.setAction(actionMap.get("bukaTransaksi")); // NOI18N
        okButton.setMnemonic('O');
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                okButtonKeyPressed(evt);
            }
        });

        postedCheckBox.setMnemonic('P');
        postedCheckBox.setText(resourceMap.getString("postedCheckBox.text")); // NOI18N
        postedCheckBox.setName("postedCheckBox"); // NOI18N
        postedCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                postedCheckBoxActionPerformed(evt);
            }
        });

        postedDateField.setEnabled(false);
        postedDateField.setName("postedDateField"); // NOI18N

        postedOpenButton.setAction(actionMap.get("openPostedOnDate")); // NOI18N
        postedOpenButton.setMnemonic('a');
        postedOpenButton.setName("postedOpenButton"); // NOI18N
        postedOpenButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                postedOpenButtonKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout controlPanelLayout = new javax.swing.GroupLayout(controlPanel);
        controlPanel.setLayout(controlPanelLayout);
        controlPanelLayout.setHorizontalGroup(
            controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controlPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(postedCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(postedDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(postedOpenButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                .addComponent(okButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(batalButton)
                .addContainerGap())
        );

        controlPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {batalButton, okButton});

        controlPanelLayout.setVerticalGroup(
            controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(controlPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(batalButton)
                    .addComponent(okButton)
                    .addComponent(postedCheckBox)
                    .addComponent(postedDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(postedOpenButton))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(controlPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(controlPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void openTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openTableMouseClicked
    if (openTable.getSelectedRowCount() == 1) {
        if (evt.getClickCount() == 2) {
            bukaTransaksi();
        }
        okButton.setEnabled(true);
    } else {
        okButton.setEnabled(false);
    }
}//GEN-LAST:event_openTableMouseClicked

private void postedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_postedCheckBoxActionPerformed
    if (postedCheckBox.isSelected()) {
        postedDateField.setEnabled(true);
        postedOpenButton.setEnabled(true);
        postedDateField.setDate(new Date());
    } else {
        postedDateField.setEnabled(false);
        postedOpenButton.setEnabled(false);
    }
}//GEN-LAST:event_postedCheckBoxActionPerformed

private void postedOpenButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_postedOpenButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        openPostedOnDate();
    }
}//GEN-LAST:event_postedOpenButtonKeyPressed

private void okButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_okButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        bukaTransaksi();
    }
}//GEN-LAST:event_okButtonKeyPressed

private void batalButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_batalButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        batalBukaTransaksi();
    }
}//GEN-LAST:event_batalButtonKeyPressed

    @Action
    public void bukaTransaksi() {
        setOpenedDate(openTable.getValueAt(openTable.getSelectedRow(), 0));
        setOpenedInvoice(openTable.getValueAt(openTable.getSelectedRow(), 1));
        setOpenedCustomer(openTable.getValueAt(openTable.getSelectedRow(), 2));
        setConfirmOpen(true);
        dispose();
    }

    @Action
    public void batalBukaTransaksi() {
        setConfirmOpen(false);
        dispose();
    }

    @Action
    public void openPostedOnDate() {
        formatOpenTable();
    }

    private void setPostedVisibility() {
        if (cfg.isRestricted(1101)) {
            postedCheckBox.setVisible(false);
            postedDateField.setVisible(false);
            postedOpenButton.setVisible(false);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton batalButton;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JPanel controlPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JScrollPane openScrollPanel;
    private javax.swing.JTable openTable;
    private javax.swing.JCheckBox postedCheckBox;
    private org.jdesktop.swingx.JXDatePicker postedDateField;
    private javax.swing.JButton postedOpenButton;
    // End of variables declaration//GEN-END:variables
}
